package com.bite.forum.service.impl;

import com.bite.forum.common.AppResult;
import com.bite.forum.common.ResultCode;
import com.bite.forum.dao.ArticleMapper;
import com.bite.forum.exception.ApplicationException;
import com.bite.forum.model.Article;
import com.bite.forum.model.Board;
import com.bite.forum.model.User;
import com.bite.forum.service.IArticleService;
import com.bite.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private BoardServiceImpl boardService;


    @Override
    public void create(Article article) {
        if(article == null || article.getUserId() == null || article.getBoardId() == null
             ||StringUtil.isEmpty(article.getTitle())
            || StringUtil.isEmpty(article.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        article.setVisitCount(0);
        article.setLikeCount(0);
        article.setReplyCount(0);
        article.setDeleteState((byte) 0);
        article.setState((byte) 0);
        Date date = new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);

        //写入数据库
        int row = articleMapper.insertSelective(article);
        if(row < 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        //增加用户的帖子数量
        User user = userService.selectByPrimaryKey(article.getUserId());
        if (user == null) {
            log.warn(ResultCode.FAILED_CREATE.toString() + ", 发贴失败, user id = " + article.getUserId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        userService.addArticleCountById(user.getId());

        //新增板块帖子数量
        Board board = boardService.selectByPrimaryKey(article.getBoardId());
        // 是否在数据库在有对应的版块
        if (board == null) {
            log.warn(ResultCode.FAILED_CREATE.toString() + ", 发贴失败, board id = " + article.getBoardId());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        // 更新版块中的帖子数量
        boardService.addArticleCountById(board.getId());

        // 打印日志
        log.info(ResultCode.SUCCESS.toString() + ", user id = " + article.getUserId()
                + ", board id = " + article.getBoardId() + ", article id = "+article.getId() + "发帖成功");

    }

    @Override
    public List<Article> selectAll() {
        List<Article> articles = articleMapper.selectAll();

        return  articles;
    }

    @Override
    public List<Article> selectAllBoardId(Long boardId) {
        if(boardId == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }

        Board board = boardService.selectByPrimaryKey(boardId);
        if(board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());

            throw new ApplicationException(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
        }

        List<Article> article = articleMapper.selectAllBoardId(boardId);
        return article;
    }

    @Override
    public Article selectDetailArticle(Long id) {
        if(id == null){
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
        }

        Article article = articleMapper.selectDetailArticle(id);

        if(article == null){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());

            throw new ApplicationException(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
        }

        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setVisitCount(article.getVisitCount() + 1);

        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(ResultCode.FAILED.getMessage());
        }
        return article;
    }

    @Override
    public void modify(Long id, String title, String content) {

        if(id == null || id < 1 || StringUtil.isEmpty(title) || StringUtil.isEmpty(content)){
            log.warn(ResultCode.FAILED.toString());

            throw new ApplicationException(ResultCode.FAILED.getMessage());
        }

        Article articles = new Article();
        articles.setId(id);
        articles.setTitle(title);
        articles.setContent(content);
        articles.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(articles);
        if(row <= 0) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
        }
    }

    @Override
    public Article selectById(Long id) {
        if(id == null){
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
        }

        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void thumbsUpById(Long id) {
        if(id == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }
        Article article = articleMapper.selectByPrimaryKey(id);

        if(article == null || article.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());

            throw new ApplicationException(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
        }

        if(article.getState() == 1){
           log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());

           throw new ApplicationException(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
        }

        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setLikeCount(article.getLikeCount() + 1);

        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);

        if(row <= 0){
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
        }

        log.warn(ResultCode.SUCCESS.toString());
    }

    @Override
    public void deleteById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);

        if(article == null || article.getState() == 1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());

            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setDeleteState((byte)1);

        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row <= 0){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        log.info(ResultCode.SUCCESS.toString());
    }

    @Override
    public void addArticleReplyCountById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());

            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getState() == 1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());

            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount() + 1);

        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);

        if(row <= 0){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
        }
    }
    @Override
    public List<Article> selectByUserId(Long userId) {
        if(userId == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Article> articles = articleMapper.selectByUserId(userId);

        return articles;
    }
}












